package solution.T0125_2_DessertShop.tax;

public class LadderTaxCalculator implements TaxCalculator {

    private final double[][] taxLadder;

    public LadderTaxCalculator(double[][] taxLadder) {
        this.taxLadder = taxLadder;
    }

    @Override
    public double calculate(double income) {
        double totalTax = 0.0; // 总税金

        for (int i = 0; i < taxLadder.length; i++) {
            double start = taxLadder[i][0]; // 当前阶梯的起点
            double rate = taxLadder[i][1]; // 当前阶梯的税率

            double end = (i + 1 < taxLadder.length) ? taxLadder[i + 1][0] : Double.MAX_VALUE; // 下一阶梯起点
            if (income <= start) {
                break; // 收入低于当前阶梯起点，结束计算
            }

            // 当前阶梯应纳税的收入
            double taxableIncome = Math.min(income - start, end - start);
            totalTax += taxableIncome * rate; // 累计税金
        }

        return totalTax;
    }
}
